package com.newtouch.krd_app.view;

import static com.newtouch.krd_app.util.ConstUtil.WidgetViewPdfDelayTime;
import static com.newtouch.krd_app.util.ConstUtil.WidgetViewPdfPeriodTime;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.newtouch.krd_app.R;
import com.newtouch.krd_app.http.api.TerminalApi;
import com.newtouch.krd_app.util.ControllerInterface;
import com.newtouch.krd_app.util.CrashHandler;
import com.newtouch.krd_app.util.DeviceUtil;
import com.newtouch.krd_app.util.MusicServer;
import com.newtouch.krd_app.util.RecyclerViewScrollHelper;
import com.newtouch.krd_app.view.adapter.PdfViewAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * author : TQ
 * time   : 2023/2/14
 * desc   : PDF组件
 */
public class WidgetViewPdf extends WidgetView{

    private PdfViewAdapter pdfViewAdapter;
    List<TerminalApi.Bean.FilesDTO.PicFilesDTO> mData;
    private RecyclerView listView;
    private int position;

    public WidgetViewPdf(Context context) {
        super(context);
    }

    public WidgetViewPdf(Context context, ControllerInterface controllerInterface) {
        super(context, controllerInterface);
    }

    @Override
    public View initView() {
        view = View.inflate(context, R.layout.widget_pdf_view,null);
        listView = view.findViewById(R.id.listView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        listView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listView.setItemAnimator(new DefaultItemAnimator());
        mData = new ArrayList<>();
        pdfViewAdapter = new PdfViewAdapter(mData);
        listView.setAdapter(pdfViewAdapter);
        return view;
    }

    @Override
    public void startPlay() {
        super.startPlay();
        int screenWidth = DeviceUtil.getScreenWidth((Activity) context);
        int screenHeight = DeviceUtil.getScreenHeight((Activity) context);
        CrashHandler.getInstance().towriteLog("filesize:"+mData.size());
        //判空，terminatePlay时会对imageTimer置空
        if (timer == null){
            timer = new Timer();
            timerTask = new TimerTask() {
                @Override
                public void run() {

                    ((Activity)context).runOnUiThread(() -> {
                        int size = mData.size();
//                        controllerInterface.playEnd();
                        if (size > 0 && position < size){
                            if (screenHeight>screenWidth){
                                if (size>3){
                                    position = position+3;
                                    if (position<=size){

                                        RecyclerViewScrollHelper.scrollToPosition(listView,position);
                                    }else{
                                        int n = size%3;
                                        position = position-3+n;
                                        if (position<size){
                                            RecyclerViewScrollHelper.scrollToPosition(listView,position);
                                        }else{
                                            //结束播放
                                            controllerInterface.playEnd();
                                            Log.e("pdfview","93 pdfview playend");
                                            CrashHandler.getInstance().towriteLog("93 pdfview playend");
                                        }

                                    }
                                }else {
                                    //结束播放
                                    controllerInterface.playEnd();
                                    Log.e("pdfview","100 pdfview playend");
                                    CrashHandler.getInstance().towriteLog("100 pdfview playend");
                                }
                            }else{
                                RecyclerViewScrollHelper.scrollToPosition(listView,position++);
                            }
                        }else{
                            //结束播放
                            controllerInterface.playEnd();
                            Log.e("pdfview","108 pdfview playend");
                            CrashHandler.getInstance().towriteLog("108 pdfview playend");
                        }
                    });
                }
            };
            //delay 4000不立即执行，等第一张显示4秒后执行
            //period 4000 间隔4秒执行循环
            timer.schedule(timerTask,WidgetViewPdfDelayTime,WidgetViewPdfPeriodTime);
            MusicServer.play(context,R.raw.background);
        }
    }

    @Override
    public void terminatePlay() {
        super.terminatePlay();
        MusicServer.stop(context);
        listView.clearOnChildAttachStateChangeListeners();
        listView.clearOnScrollListeners();
        listView.removeAllViews();
        listView = null;
        view = null;
    }

    @Override
    public void stopPlay() {
        super.stopPlay();
        MusicServer.stop(context);
        listView.clearOnChildAttachStateChangeListeners();
        listView.clearOnScrollListeners();
        listView.removeAllViews();
        listView = null;
        view = null;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void setFilesDTO(TerminalApi.Bean.FilesDTO filesDTO) {
        super.setFilesDTO(filesDTO);
        mData = filesDTO.getPicFiles();
        pdfViewAdapter.setmDatas(mData);
        pdfViewAdapter.notifyDataSetChanged();
    }

    @Override
    public int getDuration() {
        return (int) (WidgetViewPdfPeriodTime*mData.size());
    }
}
